<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="formInline">
          <el-form-item class="dateStyle">
            <el-date-picker
                    size="mini"
                    v-model="formInline.startTime"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="请选择开始日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-select clearable size="mini" v-model="formInline.status" placeholder="请选择状态">
              <el-option label="已归还" value="1"></el-option>
              <el-option label="未归还" value="0"></el-option>
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="buttonList">
      <el-col>
        <el-button type="primary" :disabled="btnDisabled" size="mini" @click="newAdd">审核</el-button>
      </el-col>
    </el-row>
    <el-row>
      <!--<el-col :span="4">
        <el-tree accordion highlight-current :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </el-col>-->
      <el-col :span="24">
        <el-table
                :data="tableData"
                size="mini"
                @selection-change="handleSelectionChange"
                border
                style="width: 100%">
          <el-table-column
                  align="center"
                  type="selection"
                  width="55">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="CLASSESNAME"
                  label="使用班级">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="USERNAME"
                  width="100px"
                  label="预约人">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="STARTTIME"
                  label="预约时间">
            <template slot-scope="scope">
              {{new Date(scope.row.STARTTIME).toLocaleString()}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ENDTIME"
                  label="归还时间">
            <template slot-scope="scope">
              {{new Date(scope.row.ENDTIME).toLocaleString()}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="CLASSIFICATIONNAME"
                  label="设备归类">
          </el-table-column>
          <!-- <el-table-column
                  align="center"
                  prop="MODEL"
                  width="180px"
                  label="数量">
          </el-table-column> -->
          <el-table-column
                  align="center"
                  prop="CREATETIME"
                  label="创建时间">
            <template slot-scope="scope">
              {{new Date(scope.row.CREATETIME).toLocaleString()}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="STATUS"
                  label="状态">
            <template slot-scope="scope">
              {{reStatus(scope.row.VERIFYSTATUS)}}
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                
                :current-page.sync="pagination.pageNo"
                layout="total, sizes, prev, pager, next, jumper"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                :page-sizes="[10, 20, 30, 50, 100]"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="预约审核" :visible.sync="dialogFormVisible" center width="40%">
      <el-form :model="form" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="18">
            <el-form-item label="使用班级">
              <el-input size="small" v-model="form.classesName" autocomplete="off" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="预约人">
              <el-input size="small" v-model="form.userName" autocomplete="off" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="预约时间">
              <el-date-picker
                type="datetime"
                placeholder="预约时间"
                v-model="form.startTime"
                :disabled="form.status !== 2"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="归还时间">
              <el-date-picker
                type="datetime"
                placeholder="归还时间"
                v-model="form.endTime"
                :disabled="form.status !== 5"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="设备归类">
              <el-input size="small" v-model="form.classificationName" autocomplete="off" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="18">
            <el-form-item label="数量">
              <el-input size="small" v-model="form.repairName" autocomplete="off" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit('pass', 1, 4)">通 过</el-button>
        <el-button size="mini" type="primary" @click="submit('back', 0, 3)">不通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as dayjs from 'dayjs'

  export default {
    name: "maintenance",
    data() {
      return{
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
        dialogImageUrl: '',
        dialogVisible: false,
        form: {
          classesName: null,
          classesId: null,
          userName: null,
          startTime: null,
          endTime: null,
          classificationName: null,
          status: null,
          id: null,
        },
        data: [],
        defaultProps: {
          children: 'list',
          label: 'NAME'
        },
        pagination: {
          pageSize: 20,
          total: 1,
          pageNo: 1
        },
        dialogFormVisible: false,
        formLabelWidth: '120px',
        formInline: {},
        tableData: [],
        btnDisabled: true,
        tableVal: []
      }
    },
    created() {
      this.getData(this.pagination.pageSize, this.pagination.pageNo);
      this.getTreeData()
    },
    methods: {
      reStatus(status) {
        const obj = {
          '0' : '预约申请审核不通过',
          '1' : '预约申请审核通过',
          '2' : '预约申请待审核',
          '3' : '延期申请审核不通过',
          '4' : '延期申请审核通过',
          '5' : '延期申请待审核',
        }
        return obj[`${status}`] || ''
      },
      getTreeData() {
        this.$post('/public/findStructureByTestBase').then(res => {
          this.data = res.data.data;
        })
      },
      search() {
        this.$post('/insrtumentAppointment/findInstrumentAppointment', {
          pageSize:this.pagination.pageSize,
          pageNo: 1,
          startTime: this.formInline.startTime,
        }).then(res => {
          if(res.data.status != 1) {
            this.$message.error('条件输入错误，请检查');
            return;
          }
          this.pagination.total = res.data.data.total;
          this.tableData = res.data.data.dataList;
        })
      },
      getData(pageSize, pageNo) {
        this.pagination.pageSize = pageSize
        this.pagination.pageNo = pageNo
        this.$post('/insrtumentAppointment/findInstrumentAppointment', {pageSize, pageNo, isNormal: 0}).then(res => {
          this.tableData = res.data.data.dataList;
          this.pagination.total = res.data.data.total;
        })
      },
      newAdd() {
        if (![2, 5].includes(this.tableVal[0].VERIFYSTATUS)) return this.$message.error('只有待审核状态才可审核');
        this.form.classesName = this.tableVal[0].CLASSESNAME;
        this.form.classesId = this.tableVal[0].CLASSESID;
        this.form.userName = this.tableVal[0].USERNAME;
        this.form.startTime = this.tableVal[0].STARTTIME;
        this.form.endTime = this.tableVal[0].ENDTIME;
        this.form.classificationName = this.tableVal[0].CLASSIFICATIONNAME;
        this.form.status = this.tableVal[0].VERIFYSTATUS;
        this.form.id = this.tableVal[0].ID;
        this.dialogFormVisible = true;
      },
      submit(type, yue, yan) {
        const req = {
          id: this.form.id,
          startTime: dayjs(this.form.startTime).format('YYYY-MM-DD HH:mm:ss'),
          endTime: dayjs(this.form.endTime).format('YYYY-MM-DD HH:mm:ss'),
          status: type === 'pass' ? (this.form.status === 2 ? yue : yan) : (this.form.status === 2 ? yue : yan),
        }
        this.$post('/insrtumentAppointment/update', req).then(res => {
          if(res.data.status === 1) {
            this.$message.success('请求成功');
            this.getData(20, 1);
            this.dialogFormVisible = false;
          } else {
            this.$message.error(res.data.msg)
          }
        })
      },
      handleSizeChange(size) {
        this.pagination.pageSize = size;
        this.getData(size, this.pagination.pageNo);
      },
      handleCurrentChange(page) {
        this.pagination.pageNo = page;
        this.getData(this.pagination.pageSize, page);
      },
      handleSelectionChange(val) {
        if (val.length === 1) {
          this.btnDisabled = false;
        }else{
          this.btnDisabled = true;
        }
        this.tableVal = val;
      },
      handleNodeClick(data, node) {
        console.log(data);
        let departmentId = '';
        let testBaseId = '';
        let testId = '';
        if(node.level === 1) {
          departmentId = data.ID;
        }
        if(node.level === 2) {
          testBaseId = data.ID;
        }
        if(node.level === 3) {
          testId = data.ID;
        }
        this.$post('/insrtument/findInstrument',{
          departmentId, testBaseId,testId,
          pageSize: this.pagination.pageSize,
          pageNo: 1
        }).then(res => {
          this.tableData = res.data.data.dataList;
          this.pagination.total = res.data.data.total;
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .search{
    margin-bottom: 10px;
  }
  /deep/.el-select{
    width: 100%;
  }
  .addDev {
    /deep/.el-form-item__content {
      display: flex;
    }
  }
  /deep/.el-divider{
    background: #409eff;
  }
  /deep/.el-divider__text{
    font-size: 18px;
    color: #409eff;
  }
  /deep/.el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%
  }
  /deep/ .el-date-editor .el-input__inner {
    padding-left: 30px !important;
  }
</style>
